// pages/setting/setting.js
import UserService from '../../../service/UserService.js'
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [
      { name: 'USA', value: '以订单位置为起点' },
      { name: 'CHN', value: '以我现在的位置为起点' },
    ],
    config: app.globalData.config,
    user: app.globalData.user
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.userService = new UserService();
  },

  radioChange: function (e) {
    const { id } = e.target.dataset;
    const { value } = e.detail;
    const param = { id, configValue: value };
    this.updConfig('locationStart', param);
  },
  onArcAddr: function(e){
    const { id } = e.target.dataset;
    const { value } = e.detail;
    const configValue = value ? 1 : 0;
    const param = { id, configValue };
    this.updConfig('arcAddr', param);
  },
  updConfig: function(field, param){
    this.userService.updConfig(param).then(res=>{
      if(res.status == 0){
        app.globalData.config[field] = param;
      }
    });
  },
  onFree: function(e){
    const _ = this;
    const {user} = this.data;
    const { value } = e.detail;
    const isFree = value ? 1 : 0;
    const param = { id: user.id, isFree }
    this.userService.updateUser(param).then(res=>{
      if(res.status == 0){
        user.isFree = isFree;
        _.setData({ user });
      }
      
    });
  },
  onFavour: function(e){
    const _ = this;
    const { user } = this.data;
    const {value} = e.detail;
    const param = { id: user.id, baseFavour: value }
    this.userService.updateUser(param).then(res => {
      if (res.status == 0) {
        user.baseFavour = value;
        _.setData({ user });
      }
    });
  },
  onIsShowFavour: function(e){
    const _ = this;
    const { user } = this.data;
    const { value } = e.detail;
    const isShowFavour = value ? 1 : 0;
    const param = { id: user.id, isShowFavour }
    this.userService.updateUser(param).then(res => {
      if (res.status == 0) {
        user.isShowFavour = isShowFavour;
        _.setData({ user });
      }
    });
  },
  onUrgent: function(e){
    const _ = this;
    const { user } = this.data;
    const { value } = e.detail;
    const isUrgent = value ? 1 : 0;
    const param = { id: user.id, isUrgent }
    this.userService.updateUser(param).then(res => {
      if (res.status == 0) {
        user.isUrgent = isUrgent;
        _.setData({ user });
      }
    });
  }
})